package email

import (
	"strings"
	"net/smtp"
	"fmt"
	"net/url"
)

var (
	eamilsList = [][4]string{
		{"a@quexianwei.com", "Qxwqxw123", "smtp.quexianwei.com", "smtp.quexianwei.com:25"},//邮件服务器配置
	}
)

func SendEmail(title, content, touser string, i ...int) {
	var index int
	if len(i) > 0 {
		index = i[0]
	}
	host := eamilsList[index][3]
	to := strings.Split(touser, ";") //收件人  ;号隔开
	content_type := "Content-Type: text/html; charset=UTF-8"
	msg := []byte("To: " + touser + "\r\nFrom: " + eamilsList[index][0] + "\r\nSubject:" + title + "\r\n" + content_type + "\r\n\r\n" + content)
	err := smtp.SendMail(host, smtp.PlainAuth("", eamilsList[index][0], eamilsList[index][1], eamilsList[index][2]), eamilsList[index][0], to, []byte(msg))

	if err != nil {
		if index < len(eamilsList)-1 {
			SendEmail(title, content, touser, index+1)
		}
		fmt.Println(err)
	}
}
